package demo1;

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @param m int整型 
     * @return int整型
     */
    public int LastRemaining_Solution (int n, int m) {
        Queue<Integer> queue = new LinkedList<>();
        for(int i = 0; i < n; i++) { queue.add(i);}

        while(queue.size() != 1){
            int count = m - 1;
            while(count != 0){
                int x = queue.poll();
                queue.add(x);
                count--;
            }
            queue.poll();
        }
        return queue.poll();
    }
    public int LastRemaining_Solution2 (int n, int m) {
        // write code here
        int f = 0;

        for(int i = 2; i <= n; i++){
            f = (f + m) % i;
        }
        return f;
    }
}